import 'package:enjoy_home/pages/home/home_page.dart';
import 'package:enjoy_home/pages/mine/mine.dart';
import 'package:enjoy_home/utils/toast.dart';
import 'package:enjoy_home/utils/token_manager.dart';
import 'package:flutter/material.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({Key? key}) : super(key: key);

  @override
  _TabBarPageState createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  // 当前选择页面对应索引
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () async {
      await TokenManager().init();
      ToastUtil.showInfo(TokenManager().getToken()!);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        // 根据索引去切换
        child: IndexedStack(
          index: _currentIndex,
          children: [HomePage(), Mine()],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        onTap: (value) => {
          setState(() {
            _currentIndex = value;
          }),
        },
        currentIndex: _currentIndex,
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
          BottomNavigationBarItem(icon: Icon(Icons.person), label: '我的'),
        ],
      ),
    );
  }
}
